//
//  SliderView.swift
//  Pods
//
//  Created by xiute on 2018/10/30.
//

import UIKit

public protocol SliderProtocol: NSObjectProtocol {
    func numberOfCount(in sliderView: SliderView) -> Int
    func slider(_ sliderView: SliderView, itemOf number: Int) -> UIViewController
    func slider(_ sliderView: SliderView, didScrollAt number: Int) -> Void
}

public class SliderView: UIView, UIScrollViewDelegate {
    weak public var delegate: SliderProtocol?
    var childrenViewControllers: Array<UIViewController> = []
    public override init(frame: CGRect) {
        super.init(frame: frame)
        self.addSubview(self.scrollView)
    }
    
    public func reloadData() -> Void {
        self.childrenViewControllers = []
        self.handleItemCount()
        self.handleSubViews()
    }
    
    public func didSelect(_ index: Int) {
        let width = self.bounds.size.width
        UIView.animate(withDuration: 0.25) {
            self.scrollView.contentOffset = CGPoint.init(x: CGFloat(index) * width, y: 0)
        }
    }
    
    //MARK: private function
    private func handleItemCount() -> Void {
        let count = self.delegate?.numberOfCount(in: self)
        self.scrollView.contentSize = CGSize.init(width: self.bounds.size.width * CGFloat(count!), height: self.bounds.size.height)
    }
    
    private func handleSubViews() -> Void {
        let count = self.delegate?.numberOfCount(in: self)
        for i in 0 ..< count! {
            let vc = self.delegate?.slider(self, itemOf: i)
            vc?.view.frame = CGRect.init(x: CGFloat(i) * self.bounds.size.width, y: 0, width: self.bounds.size.width, height: self.bounds.size.height)
            self.scrollView.addSubview((vc?.view)!)
            vc?.viewDidLoad()
            if i == 0 {
                vc?.viewWillAppear(true)
                vc?.viewDidAppear(true)
            }
            self.childrenViewControllers.append(vc!)
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: ScrollView delegate
    private func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
        let index = Int(scrollView.contentOffset.x) / Int(self.bounds.size.width)
        let currentVC = self.childrenViewControllers[index]
        currentVC.viewWillDisappear(true)
        currentVC.viewDidDisappear(true)
        
    }
    //这个回调不会被执行 不知道是不是swift的问题 不过在scrollViewDidScroll中做回调也没有问题
    private func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        let index = Int(scrollView.contentOffset.x) / Int(self.bounds.size.width)
        let currentVC = self.childrenViewControllers[index]
        currentVC.viewWillAppear(true)
        currentVC.viewDidAppear(true)
        self.delegate?.slider(self, didScrollAt: index)
    }
    
    public func scrollViewDidScroll(_ scrollView: UIScrollView) {
        let index = Int(scrollView.contentOffset.x) / Int(self.bounds.size.width)
        let currentVC = self.childrenViewControllers[index]
        currentVC.viewWillAppear(true)
        currentVC.viewDidAppear(true)
        self.delegate?.slider(self, didScrollAt: index)
    }
    
    
    public func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
        if !decelerate {
            let index = Int(scrollView.contentOffset.x) / Int(self.bounds.size.width)
            let currentVC = self.childrenViewControllers[index]
            currentVC.viewWillAppear(true)
            currentVC.viewDidAppear(true)
            self.delegate?.slider(self, didScrollAt: index)
        }
    }
    
    //MARK: property lazy load
    lazy var scrollView: UIScrollView = {
        let scrollView = UIScrollView(frame: self.bounds)
        scrollView.isPagingEnabled = true
        scrollView.showsVerticalScrollIndicator = false
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.bounces = false
        scrollView.delegate = self;
        return scrollView
    }()
}
